//
//  YTAuthorListController.m
//  yitaicaijing
//
//  Created by DIANGHONG on 2018/7/27.
//  Copyright © 2018年 DIANGHONG. All rights reserved.
//

#import "YTAuthorListController.h"
#import "YTFollowAutherCell.h"

NSString *authorListCellID = @"authorListCellID";
@interface YTAuthorListController ()
@property(nonatomic ,strong)YTFollowAuthorModel *model;

@end

@implementation YTAuthorListController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setupUI];
}


#pragma mark: - 返回按钮 监听
- (void)backButtonClick
{
    if (UIApplication.sharedApplication.keyWindow.rootViewController == self) {
        UIApplication.sharedApplication.keyWindow.rootViewController = [[YTMainViewController alloc] init];
        return;
    }
    [self.navigationController popViewControllerAnimated:true];
}


- (void)setupUI
{
    // setUpNav
    self.navigationItem.title = @"作者列表";
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"倛袨1"]
                                                                             style:UIBarButtonItemStylePlain
                                                                            target:self action:@selector(backButtonClick)];
    [self.navigationItem.leftBarButtonItem setTintColor:kMainColor];
    
    // setup refresh
    YTRefreshHeader *header = [YTRefreshHeader headerWithRefreshingBlock:^{
        [self loadDataWithPullup:NO];
    }];
    
    MJRefreshAutoNormalFooter *footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        [self loadDataWithPullup:YES];
    }];
    [footer setTitle:@"没有更多了哦~" forState:MJRefreshStateNoMoreData];
    [footer setTitle:@"" forState:MJRefreshStateIdle];
    self.tableView.mj_header = header;
    self.tableView.mj_footer = footer;
    self.tableView.backgroundColor = [UIColor colorWithHex:@"f1f2f2"];
    

    // setup table view
    [self.tableView registerNib:[UINib nibWithNibName:@"YTFollowAutherCell" bundle:nil] forCellReuseIdentifier:authorListCellID];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.rowHeight = 74;
    
    
    [self.tableView.mj_header beginRefreshing];
    self.tableView.mj_header.state = MJRefreshStatePulling;
}

#pragma mark； - 加载数据
- (void)loadDataWithPullup:(BOOL)pullup
{
    int page = self.model ? self.model.page.intValue : 1;
    
    if (pullup) {
        page++;
        if (self.model.list.count >= self.model.total.intValue) {
            [self.tableView.mj_footer endRefreshingWithNoMoreData];
            return;
        }
    }else{
        page = 1;
    }
    
    [YTNetworkManager.shareManager requestFollowListWithIsAuthorList:YES paged:page
                                                          completion:^(YTFollowAuthorModel *model, BOOL isSucc, NSString *error)
     {
         if (!isSucc) {
             [self.view showTipWithTipStr:error];
             pullup ? [self.tableView.mj_footer endRefreshing] : [self.tableView.mj_header endRefreshing];
             return ;
         }
         
         if (pullup) {
             self.model.list = [self.model.list arrayByAddingObjectsFromArray:model.list];
             [self.tableView.mj_footer endRefreshing];
         }else{
             self.model = model;
             
             [self.tableView.mj_header endRefreshing];
         }
         
         error ? nil : [self.tableView reloadData];
     }];
}

#pragma mark: - table view data source
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.model.list.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    YTFollowAutherCell *cell = [tableView dequeueReusableCellWithIdentifier:authorListCellID
                                                               forIndexPath:indexPath];
    cell.isAuthorList = YES;
    cell.model = self.model.list[indexPath.row];
   
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.tag = indexPath.row;
    cell.toFollowAuthorBlock = ^(NSInteger idx) {
        
        [self loadDataWithPullup:NO];
    };
    
    cell.authorIconClickBlock = ^(NSString *authorID) {

        YTAuthorInfoController *infoVC = [[YTAuthorInfoController alloc] init];
        infoVC.user_id = authorID;
        [self.navigationController pushViewController:infoVC animated:true];
    };
    return cell;
}
@end
